package com.tiantian.quanminxiu.api;

import android.content.Context;
import android.util.Log;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.event.SocketGiftEvent;
import com.tiantian.quanminxiu.event.SocketMsgEvent;
import com.tiantian.quanminxiu.event.SocketStateEvent;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;

/**
 * Created by admin on 2016/8/10.
 * 此Socket工具中写入了所有的接收消息的监听。通过Handle返回数据。
 */
public class SocketUtils {

    public static final int CONNECT = 1000;
    public static final int DISCONNECT = 1001;
    public static final int CONNECTERROR = 1002;
    public static final int LIVELINSENER = 1003;
    public static final int BLACKSETBROADCAST = 1004;
    public static final int USERLEVElBROADCASTLINSENER = 1006;
    public static final int POMELOGETLINSENER = 1007;
    public static final int POMELOPRESENTLINSENER = 1008;
    public static final int POMELOPRESENTBROADCASTLINSENER = 1009;
    public static final int GIFTLINSENER = 1010;
    public static final int GIFTBROADCASTLINSENER = 1011;
    public static final int MESSAGELINSENER = 1012;
    public static final int MESSAGEBROADCASTLINSENER = 1013;
    public static final int JOINBROADCASTLINSENER = 1014;
    public static final int USERLISTLINSENER = 1015;
    public static final int JOINLINSENER = 1016;
    public static final int OUTBROADCASTLINSENER = 1017;
    public static final int PONDBROADCASTLINSENER = 1018;
    public static final int RUNWAYBROADCASTLINSENER = 1019;
    public static final int LIVEBROADCASTLINSENER = 1020;
    public static final int GUARDIANLINSENER = 1022;
    public static final int GUARDIANBROADCASTLINSENER = 1023;
    public static final int HORNLINSENER = 1024;
    public static final int HORNBROADCASTLINSENER = 1025;
    public static final int GUARDIANLISTLINSENER = 1026;
    public static final int ADMINLISTLINSENER = 1027;
    public static final int BLACKLISTLINSENER = 1028;
    public static final int WARNINGSETBROADCAST = 1029;

    private static SocketUtils mSocketUtils;
    private Context context;
    private Socket mSocket;

    private SocketUtils(){}

    //region getInstance
    public static SocketUtils getInstance(){
        if(mSocketUtils==null){
            mSocketUtils = new SocketUtils();
        }
        return mSocketUtils;
    }
    //endregion

    //region 绑定固定界面 bindActivity()
    public void bindActivity(Context context){
        this.context = context;
        initSocket();
    }
    //endregion

    //region 初始化Socket连接 initSocket()
    private void initSocket(){
        if(mSocket==null){
            try {
                mSocket = IO.socket(Constants.SOCKET_URL+"room");
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        mSocket.on(Socket.EVENT_CONNECT, onConnect);
        mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
        mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
        mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
        mSocket.on("Join", JoinLinsener);
        mSocket.on("JoinBroadcast", JoinBroadcastLinsener);
        mSocket.on("AdminList", AdminListLinsener);
        mSocket.on("BlackList", BlackListLinsener);
        mSocket.on("UserList", UserListLinsener);
        mSocket.on("Message", MessageLinsener);
        mSocket.on("MessageBroadcast", MessageBroadcastLinsener);
        mSocket.on("Live", LiveLinsener);
        mSocket.on("LiveBroadcast", LiveBroadcastLinsener);
        mSocket.on("Guardian", GuardianLinsener);
        mSocket.on("GuardianBroadcast", GuardianBroadcastLinsener);
        mSocket.on("GuardianList", GuardianListLinsener);
        mSocket.on("Gift", GiftLinsener);
        mSocket.on("GiftBroadcast", GiftBroadcastLinsener);
        mSocket.on("PomeloGet", PomeloGetLinsener);
        mSocket.on("PomeloPresent", PomeloPresentLinsener);
        mSocket.on("PomeloPresentBroadcast", PomeloPresentBroadcastLinsener);
        mSocket.on("PondBroadcast", PondBroadcastLinsener);
        mSocket.on("RunwayBroadcast", RunwayBroadcastLinsener);
        mSocket.on("HornBroadcast", HornBroadcastLinsener);
        mSocket.on("Horn", HornLinsener);
        mSocket.on("OutBroadcast", OutBroadcastLinsener);
        mSocket.on("UserLevelBroadcast", UserLevelBroadcast);

        mSocket.on("BlackSetBroadcast", BlackSetBroadcast);
        mSocket.on("WarningSetBroadcast", WarningSetBroadcast);

//        mSocket.on("BlackSet", BlackSetLinsener);
//        mSocket.on("BlockSet", BlockSetLinsener);
//        mSocket.on("AdminSetBroadcast", AdminSetBroadcast);
//        mSocket.on("AdminSet", AdminSetLinsener);
    }
    //endregion

    //region 清除某个监听 clearListen(String event)
    public void clearListen(String event){
        mSocket.off(event);
    }
    //endregion

    //region 断开连接并清除所有监听 clearAllListen
    public void clearAllListen(){
        mSocket.disconnect();
        mSocket.off();
    }
    //endregion

    //region 消毁Socket distroySocket
    public void distroySocket() {
        if (null != mSocket) {
            clearAllListen();
            context = null;
            mSocket.close();
            mSocket = null;
        }
    }
    //endregion

    //region 发送消息 sendMessage(final String message, final Object... args)
    public void sendMessage(final String message, final Object... args) {
        try
        {
            if (null == mSocket) {
                initSocket();
                CommonUtils.showToask(context,"链接断开,请重试...");
            } else {
                if (!mSocket.connected()) {
                    mSocket.connect();
                }
                if(CommonUtils.getNetState(context)!=-1||message.equalsIgnoreCase("Join")){
                    mSocket.emit(message, args);
                }else {
                    CommonUtils.showToask(context,"网络状态不好,请重试...");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //endregion

    //******************************以下是所有监听***********************
    //region 连接成功 onConnect
    private Emitter.Listener onConnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            EventBusUtils.getInstance().post(new SocketStateEvent(CONNECT));
        }
    };
    //endregion

    //region 断开连接监听 onDisconnect
    private Emitter.Listener onDisconnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            EventBusUtils.getInstance().post(new SocketStateEvent(DISCONNECT));
        }
    };
    //endregion

    //region 连接出现错误 onConnectError
    private Emitter.Listener onConnectError = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            EventBusUtils.getInstance().post(new SocketStateEvent(CONNECTERROR));
        }
    };
    //endregion

    //region 发送我的进房信息并监听 JoinLinsener
    private Emitter.Listener JoinLinsener = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            Log.i("SocketUtils", "--live:" + args[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(args[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(JOINLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 其他人进房监听 JoinBroadcastLinsener
    private Emitter.Listener JoinBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(JOINBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 用户列表 AdminListLinsener
    private Emitter.Listener AdminListLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:a" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(ADMINLISTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 用户列表 BlackListLinsener
    private Emitter.Listener BlackListLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:b" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(BLACKLISTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 用户列表 UserListLinsener
    private Emitter.Listener UserListLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(USERLISTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 我发送信息监听 MessageLinsener
    private Emitter.Listener MessageLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketMsgEvent(MESSAGELINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 其他人发送信息监听 MessageBroadcastLinsener
    private Emitter.Listener MessageBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketMsgEvent(MESSAGEBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 主播开播直播监听 LiveLinsener
    private Emitter.Listener LiveLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject=new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(LIVELINSENER,jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 其他主播开播直播监听 LiveBroadcastLinsener
    private Emitter.Listener LiveBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject=new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(LIVEBROADCASTLINSENER,jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 守护购买通知 GuardianLinsener
    private Emitter.Listener GuardianLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject=new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(GUARDIANLINSENER,jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 其他守监听 GuardianBroadcastLinsener
    private Emitter.Listener GuardianBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject=new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(GUARDIANBROADCASTLINSENER,jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 守护列表监听 GuardianListLinsener
    private Emitter.Listener GuardianListLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject=new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(GUARDIANLISTLINSENER,jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 我发送礼物监听 GiftLinsener
    private Emitter.Listener GiftLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketGiftEvent(GIFTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 其他人发送礼物监听 GiftBroadcastLinsener
    private Emitter.Listener GiftBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketGiftEvent(GIFTBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 得到柚子监听 PomeloGetLinsener
    private Emitter.Listener PomeloGetLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketGiftEvent(POMELOGETLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 我送柚子监听 PomeloPresentLinsener
    private Emitter.Listener PomeloPresentLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketGiftEvent(POMELOPRESENTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 其他人送柚子监听 PomeloPresentBroadcastLinsener
    private Emitter.Listener PomeloPresentBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketGiftEvent(POMELOPRESENTBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    //endregion

    //region 直播间奖池监听 PondBroadcastLinsener
    private Emitter.Listener PondBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(PONDBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 全服跑道监听 RunwayBroadcastLinsener
    private Emitter.Listener RunwayBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(RUNWAYBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 全服跑道监听 HornLinsener
    private Emitter.Listener HornLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(HORNLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 喇叭监听 HornBroadcastLinsener
    private Emitter.Listener HornBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(HORNBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 其他人退出房间监听 OutBroadcastLinsener
    private Emitter.Listener OutBroadcastLinsener = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(OUTBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 用户升级监听 UserLevelBroadcast
    private Emitter.Listener UserLevelBroadcast = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            // TODO Auto-generated method stub
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(USERLEVElBROADCASTLINSENER, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 踢人监听 BlackSetBroadcast
    private Emitter.Listener BlackSetBroadcast = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(BLACKSETBROADCAST, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion

    //region 警告事件监听 WarningSetBroadcast
    private Emitter.Listener WarningSetBroadcast = new Emitter.Listener() {

        @Override
        public void call(Object... arg0) {
            //冻结房间
            Log.i("SocketUtils", "--live:" + arg0[0].toString());
            try {
                JSONObject jsonObject = new JSONObject(arg0[0].toString());
                if(jsonObject!=null){
                    EventBusUtils.getInstance().post(new SocketStateEvent(WARNINGSETBROADCAST, jsonObject));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    //endregion
}
